/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unc.pds.util;

import java.util.ArrayList;

/**
 *
 * @author ain
 */
public class ArchImpl implements Arch {
    private String name;
    private String value;

    ArrayList childs = new ArrayList();

    public ArchImpl(String name, String val){
        this.name = name;
        this.value = val;
    }

    public Arch getChild() {
        if (childs.isEmpty()) return null;
        else return (Arch)childs.get(0);
    }

    public Arch[] getChilds() {
        Arch[] ans = new Arch[childs.size()];
        for (int i=0;i<ans.length;i++){
            ans[i]=(Arch)childs.get(i);
        }
        return ans;
    }

    public Arch getChild(String name) {
        for (int i=0;i<childs.size();i++){
            if (((Arch)childs.get(i)).getName().equalsIgnoreCase(name)) return ((Arch)childs.get(i));
        }
        return null;
    }

    public Arch[] getChilds(String name) {
        ArrayList preans = new ArrayList();
        for (int i=0;i<childs.size();i++){
            if (((Arch)childs.get(i)).getName().equalsIgnoreCase(name)) preans.add((Arch)childs.get(i));
        }
        Arch[] ans = new Arch[preans.size()];
        for (int i=0;i<ans.length;i++){
            ans[i]=(Arch)preans.get(i);
        }
        return ans;
    }

    public void addChild(Arch child) {
        childs.add(child);
    }

    public void removeChild(Arch child) {
        childs.remove(child);
    }
    
    public void removeChild(String name) {
        ArrayList preans = new ArrayList();
        for (int i=0;i<childs.size();i++){
            if (((Arch)childs.get(i)).getName().equalsIgnoreCase(name)) preans.add(new Integer(i));
        }
        for (int i=preans.size()-1;i>=0;i--){
            childs.remove(preans.get(i));
        }
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setValue(String val) {
        this.value = val;
    }

    public String getValue() {
        return value;
    }

    public void rewriteChild(Arch child) {
        Arch exchild = getChild(child.getName());
        if (exchild == null){
            addChild(child);
        } else {
            removeChild(child.getName());
        }
        addChild(child);
    }

}
